package com.aluka.nirvana.framework.core.model;

import com.aluka.nirvana.framework.core.exception.BaseException;
import lombok.Data;

import java.io.Serializable;

/**
 * 基本响应实体
 * @author gongli
 * @param <T> 响应具体类型
 */
@Data
public class ResponseBaseEntity<T> implements Serializable {

	private static final long serialVersionUID = 1L;

	private int status = 200;
	private String message = "N/A";
	private String errorCode;
	private T body;
	private BaseExceptionBody exceptionBody;

	public ResponseBaseEntity() {
	}

	public ResponseBaseEntity(T body, int status, String message) {
		this.status = status;
		this.message = message;
		this.body = body;
	}

	public ResponseBaseEntity(String message, T body) {
		this.message = message;
		this.body = body;
	}

	public ResponseBaseEntity(T body) {
		this.body = body;
	}

	public static ResponseBaseEntity<String> success() {
		ResponseBaseEntity<String> entity = new ResponseBaseEntity<>();
		entity.setMessage("success");
		return entity;
	}

	public static <T> ResponseBaseEntity<T> success(T t) {
		ResponseBaseEntity<T> entity = new ResponseBaseEntity<>();
		entity.setMessage("success");
		entity.setBody(t);
		return entity;
	}
	
	public static ResponseBaseEntity<String> error(String message) {
		ResponseBaseEntity<String> entity = new ResponseBaseEntity<>();
		entity.setMessage(message);
		entity.setStatus(500);
		return entity;
	}

	public static ResponseBaseEntity<String> error(String message , BaseException e) {
		ResponseBaseEntity<String> entity = new ResponseBaseEntity<>();
		entity.setMessage(message);
		entity.setExceptionBody(new BaseExceptionBody(e));
		entity.setStatus(500);
		entity.setErrorCode(e.getErrorCode());
		return entity;
	}
}
